package com.yang.zngcxyxkjs.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yang.zngcxyxkjs.mapper.FavoriteMapper;
import com.yang.zngcxyxkjs.pojo.Competition;
import com.yang.zngcxyxkjs.pojo.Favorite;
import com.yang.zngcxyxkjs.pojo.Student;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class FavoriteController {

    @Autowired
    private FavoriteMapper favoriteMapper;

    @RequestMapping("/addFavorite")
    public String addFavorite(String userNo, Integer comId, String comName, String dutyTeacher, String teaPhone,
                              Model model, HttpSession session, RedirectAttributes attributes){
        Student stu = (Student)session.getAttribute("stu");
        model.addAttribute("stu",stu);

        Favorite favorite = favoriteMapper.selectFavoriteByComId(comId);
        int f = 0;
        if (favorite==null){
            boolean b = favoriteMapper.insertFavorite(userNo,comId,comName,dutyTeacher,teaPhone);
            if (b){
                f = 1;
//                model.addAttribute("f",f);
//                attributes.addAttribute("f",f);
                return "redirect:/getAllCompetitionsByPages?f=" + f;
            }else{
                return "error.html";
            }
        }else{
            f = 2;
//            model.addAttribute("f",f);
//            attributes.addAttribute("f",f);
            return "redirect:/getAllCompetitionsByPages?f=" + f;
        }
    }

    //TODO 备份
//    @RequestMapping("/addFavorite")
//    public String addFavorite(String userNo,Integer comId,String comName,String dutyTeacher,String teaPhone, Model model, HttpSession session){
//        Student stu = (Student)session.getAttribute("stu");
//        model.addAttribute("stu",stu);
//
//        Favorite favorite = favoriteMapper.selectFavoriteByComId(comId);
//        if (favorite==null){
//            boolean b = favoriteMapper.insertFavorite(userNo,comId,comName,dutyTeacher,teaPhone);
//            if (b){
//                return "success.html";
//            }else{
//                return "error.html";
//            }
//        }else{
//            return "repeat.html";
//        }
//    }


    @RequestMapping("/getAllFavoriteByUserNo")
    public String getAllFavoriteByUserNo(@RequestParam(defaultValue = "1") Integer currentPageNum, String stuNo,
                                         Model model, HttpSession session){
        PageHelper.startPage(currentPageNum,5);
        List<Favorite> list = favoriteMapper.selectAllFavoriteByUserNo(stuNo);
        PageInfo<Favorite> pageInfo = new PageInfo<>(list);

        Student stu = (Student)session.getAttribute("stu");
        model.addAttribute("stu",stu);

        model.addAttribute("pageInfo",pageInfo);
        return "student/studentFavorite.html";
    }

    @RequestMapping("/deleteFavoriteByComId")
    public String deleteFavoriteByComId(Integer comId,HttpSession session){
       boolean i =  favoriteMapper.deleteFavoriteByComId(comId);
       Student stu = (Student)session.getAttribute("stu");
       return "redirect:/getAllFavoriteByUserNo?stuNo=" + stu.getStuNo();
    }

}
